# 导入所需库
import requests
import re
import csv

# url
url = "https://movie.douban.com/top250"
# User-Agent
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/100.0.4896.127 Safari/537.36 "
}
resp = requests.get(url, headers=headers)
page_context = resp.text
print(page_context)

# 解析数据
obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)'
                 r'</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp.*?<span '
                 r'class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
                 r'<span>(?P<num>.*?)人评价</span>.*?'
                 r'<span class="inq">(?P<topic>.*?)</span>', re.S)

# 数据匹配
result = obj.finditer(page_context)
file = open("../files/doubantop250.csv", mode="w")
csvwriter = csv.writer(file)
for it in result:
    dic = it.groupdict()
    dic['year'] = dic['year'].strip()
    print(dic.values())
    csvwriter.writerow(dic.values())
file.close()
print("爬取完毕!")

# 关闭请求
resp.close()
